#include <QtGui>
// #include <qt.cpp>
#include "dlgmsg.h"


#ifdef __cplusplus
extern "C" {
#endif

DlgMsg::DlgMsg(QWidget *parent)
	: QDialog(parent)
{
	int frameStyle = 0;//= QFrame::Sunken | QFrame::Panel;

	textEdit = new QTextEdit;
	textEdit->setObjectName("ID_EDIT_MSG");

	QGridLayout *layout = new QGridLayout;



	layout->setColumnStretch(1, 1);
	layout->setColumnMinimumWidth(1, 250);

	layout->addWidget(textEdit	, 0, 1);
	setWindowFlags(Qt::WindowStaysOnTopHint | Qt::FramelessWindowHint);

	connect(this, SIGNAL(sigShow()), this, SLOT(doShow()));
	connect(this, SIGNAL(sigHide()), this, SLOT(doHide()));
	connect(this, SIGNAL(sigAppend(char *)), this, SLOT(doAppend(char *)));

	setLayout(layout);

	setWindowTitle(("Standard Dialogs"));
}

// void DlgMsg::set_net()
// {
// 	integerLabel->setText(tr("%1%").arg(0));
// 	bool ok;
// 	// int i = QInputDialog::getInt(this, ("QInputDialog::getInteger()"),
// 	//                              ("Percentage:"), 25, 0, 100, 1, &ok);
// 	printf("jjjjjjjj\n");
// 	QProcess *pro = new QProcess; 
// 	pro->start("/app/qt.elf", NULL);
// 	// pro->start("./dlg.elf", NULL);
// 	exit(1);
// }


void DlgMsg::onShow()
{
	// printf("function %s()\n", __FUNCTION__);
	emit sigShow();
}
void DlgMsg::doShow()
{
	// printf("function %s()\n", __FUNCTION__);
	show();
	
}

void DlgMsg::onHide()
{
	// printf("function %s()\n", __FUNCTION__);
	emit sigHide();
	// hide();
	
}

void DlgMsg::doHide()
{
	hide();	
}

void DlgMsg::onAppend(char *str)
{
	// printf("function %s()\n", __FUNCTION__);
	emit sigAppend(str);
	// hide();
	
}

void DlgMsg::doAppend(char *str)
{
	// printf("string %s\n", str);
	QTextEdit *pwnd = findChild<QTextEdit *>("ID_EDIT_MSG");
	pwnd->append(str);
	// pwnd->verticalScrollBar()->scroll(1, 3);
	pwnd->moveCursor(QTextCursor::End);
	free(str);
}



extern DlgMsg *g_dlgmsg;
int do_msg_append(void *ptr, int argc, char **argv)
{
	// g_dlgmsg->onShow();
	// g_dlgmsg->onHide();
	// printf("malloc [%d] \n", argc);
	char *strout;
	int len = 0;
	for (int i = 1;i < argc; i++) {
		len += strlen(argv[i]);
	}
	// printf("malloc [%d] \n", len + argc * 4);
	strout = (char*)malloc(len + argc * 4);

	memset(strout, 0, len + argc * 4);
	for (int i = 1;i < argc; i++) {
		strcat(strout, argv[i]);
		strcat(strout, " ");
	}
	strcat(strout, "\0");
	// printf("str len %d\n", strlen(strout));

	for (int i = 1;i < argc; i++) {
		// printf("%d %s \n", i, argv[i]);
	}
	// printf("argc %d argv %s [%s]\n",argc, argv[1], strout);

	// g_dlgmsg->onAppend(argv[1]);
	// g_dlgmsg->onAppend(strout);
	g_dlgmsg->onAppend(strout);

}



#ifdef __cplusplus
}
#endif